<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
  <div id="app">
    {{firstname}} {{lastname}}
    <hr>
    {{getfullname}} - {{getfullname}}
    <br>
    {{getfullname1()}} - {{getfullname1()}}

    <button v-on:click="getfullname='tao'">修改lastname</button>

  </div>
  <script>

    var vm = new Vue({
      el: "#app",
      data: {
        firstname: 'zhou',
        lastname: 'weitao',
        user:{
          name:'xiaoming',
          age:20
        }
      },
      //计算属性,基于现有data里面的数据，衍生出来新的属性
      //会有缓存
      computed: {
        // getfullname() {
        //   console.log('computed')
        //   return this.firstname + " " + this.lastname;
        // }
        getfullname: {
          get:function(){
            console.log('computed')
            return this.firstname + " " + this.lastname;
          },
          set:function(val){
            this.lastname = val;
          }
        }
      },
      methods: {
        getfullname1() {
          console.log('methods')
          return this.firstname + " " + this.lastname;
        }
      },
      //侦听器（观测data里面的数据的变化）
      watch: {
        lastname(newval, oldval) {
          console.log(newval, oldval)
        },
        // user(newval, oldval) {
        //   console.log(newval, oldval.name)
        // }
        user:{
          handler:function(newval,oldval){
            console.log(newval.name,oldval.name)
          },
          deep:true
        }
      }
    })

  </script>
</body>

</html>